<!-- 个人信息/基本资料 -->
<template>
	<view class="basicInformation">
		<!-- <view class="myNmae flex-between">
			<text style="min-width: 50rpx;">头像</text>
			<image @click="clk(0)" class="avatar" style="border-radius: 50%;" :src="urls[0]" mode="">

			</image>
			<avatar   style="position: absolute;" @upload="myUpload" ref="avatar"></avatar>
		</view> -->
		<view class="myNmae flex-between">
			<text style="min-width: 150rpx;">真实姓名</text>
			<input class="font-overflow" v-model="form.real_name" placeholder="请输入姓名" />
		</view>
		<view class="myNmae flex-between">
			<text style="min-width: 50rpx;">电话</text>
			<input maxlength="11" type="number" v-model="form.phone" placeholder="请输入联系方式" />
		</view>


		<view class="flex-between myNmae">
			<text>您的性别</text>
			<radio-group v-model="form.sex" name="apply" @change="handleApplyType">
				<label class="m-right10">
					<radio :checked="form.sex ==1" class="min-radio" value="1" color="#67C23A" />
					<text>男</text>
				</label>
				<label>
					<radio :checked="form.sex ==2" class="min-radio" value="2" color="#67C23A" />
					<text>女</text>
				</label>
			</radio-group>
		</view>

		<view class="myNmae flex-between">
			<text>生日</text>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
				<view class="flex-align-center color6" v-if="yeartime">
					{{ yeartime }}
					<text>-</text>
					{{ monthtime }}
					<text>-</text>
					{{ daytime }}
				</view>
				<view class="flex-align-center color6" v-else>请选择生日</view>
			</picker>
			<!-- <text>请选择从业时间</text> -->
		</view>
		<view class="myNmae flex-between">
			<text>地区</text>
			<pca-picker-cc v-on:getAddressResultObj="getAddressResultObj" :addressList.sync="addressList">
				<view class="uni-input">{{ addressList.join(' ') }}</view>
			</pca-picker-cc>
			<!-- <text>请选择从业时间</text> -->
		</view>


		<view class="sumbit-btn flex" @click="sumbmit()">提交保存</view>

	</view>
</template>

<script>
	import {
		object2Url
	} from '@/utils/trans';
	import avatar from "@/components/yq-avatar/yq-avatar.vue";

	export default {

		components: {
			avatar
		},
		data() {
			const currentDate = this.getDate({
				format: true
			});
			return {
				urls: ["/static/会员卡首页1/annoymous.png", "/static/会员卡首页1/annoymous.png"],
				form: {
					// avatar: "",
					phone: '',
					real_name: '',
					// 1 男 2女
					sex: '1',
					// 生日
					birthday: currentDate,
					province_id: "",
					city_id: '',
					area_id: ""

				},
				addressList: ["", "", ""],
				addValue: '',
				singleLabel: '', // 单个标签覆盖
				//添加标签数据
				tagAddList: [],
				//选择的年份
				yeartime: currentDate.split('-')[0],
				//选择的月份
				monthtime: currentDate.split('-')[1],
				//选择的天数
				daytime: currentDate.split('-')[2],
				date: null,

				avatarObj: {},
				token: uni.getStorageSync('login').token,

				// 上传状态
				upLoadStatus: '',
				storageMode: '',
				alyOssSignatureInfo: null,

				timeDate: '',
				suffix: '',
			};
		},
		onLoad(options) {
			// if (options.id) {
			// 	this.form.id = options.id
			// 	console.log(this.form)

			// }

		},
		onShow() {
			this.init()
			this.$at.releaseKey('submit');
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			},


		},
		methods: {
			async init() {
				
			
				const ress = await this.$http.post({
					url: this.$api.userinfo,
					data: {
			
						id: uni.getStorageSync('userId'),
			
					},
			
			
				})
				console.log(ress)
				if (ress.code == 0) {
					
			      this.$sun.assignObject(this.form, {
			      	...ress.data
			      })
			    this.form.sex=ress.data.sex?ress.data.sex:'1'
			    // sex: '1',
					 // this.$set([ress.data.img]);
				 // this.urls[0]=ress.data.avatar
			     	// this.$refs.selectimgandupload1.setItems([ress.data.img]);
			    
				 this.addressList[0] = this.form.province_name?this.form.province_name:'北京市'
				 this.addressList[1] = this.form.city_name?this.form.city_name:'市辖区'
				 this.addressList[2] = this.form.area_name?this.form.area_name:'东城区'
			
				} else {
			
					this.$sun.toast(ress.msg, 'none')
				}
			
			},
			handleApplyType(e) {
				let val = e.detail.value;
				this.form.sex = val;
				console.log(e.detail.value);
			},
			sumbmit() {
			   
				this.$at.uniDoFunc({
					key: 'submit',
					success: async () => {
						this.$at.lockKey('submit');
						// 登录 需要进行判断当前账号密码 和账号归属店铺及角色是否又选择
						if (!this.form.real_name) {
							this.$sun.toast("请输入姓名", 'none')
							this.$at.releaseKey('submit');
						} else if (!this.form.phone) {
							this.$sun.toast("请输入电话", 'none')
							this.$at.releaseKey('submit');
						}  else {


							console.log((this.form))
							const saveOrUpdate = this.isEdit ? this.$api.editClassify : this.$api
								.addClassify;
							const res = await this.$http.post({
								url: this.$api.updateUser,
								data: {

									...this.form
								},
								headers: {
									'Authorization': `Bearer ${this.token}`
								}

							})
							console.log(res)
							if (res.code == 0) {



								// 跳转到登录页面
								this.$at.releaseKey('submit');
								this.$sun.toast(res.msg, 'none')

								setTimeout(() => {
									this.$sun.navRe('/pages/firstPage/firstPage', {
										currentIndex: 0
									})
									
								}, 800)
							} else {
								this.$at.releaseKey('submit');
								this.$sun.toast(res.msg, 'none')
							}
						}

					}
				});
				console.log(this.form)
			},
			clk(index) {
				this.$refs.avatar.fChooseImg(index, {
					selWidth: "300upx",
					selHeight: "300upx",
					expWidth: '260upx',
					expHeight: '260upx'
				});
			},
			async myUpload(rsp) {
				this.$set(this.urls, rsp.index, rsp.path);
				console.log(rsp)
				this.avatarObj = rsp
				if (this.storageMode == 3) {
					uni.uploadFile({
						url: this.$api.qnyOssSignature, //仅为示例，非真实的接口地址
						filePath: this.avatarObj.path,
						name: 'file',
						formData: {

						},
						success: (res) => {
							if (res.statusCode == 200) {
								console.log('uploadImage success, res is:', res)
								uni.hideLoading();
								uni.showToast({
									title: '上传成功',
									icon: 'success',
									duration: 1000
								})
								this.upLoadStatus = '上传成功'
								this.form.avatar = JSON.parse(res.data).data.url
								console.log(this.form.avatar)
							}



						},
						fail: (err) => {
							if (this.pageVisible) {
								console.log('uploadImage fail', err);
								uni.hideLoading();
								uni.showModal({
									content: err.errMsg,
									showCancel: false
								});
							}
						},
					});
				} else {
					const ts = new Date().getTime();
					// const ua = navigator.userAgent;
					const us = {
						ts,
						// ua
					};
					let url = 'https://yjykmaster.admin168.net/door-massage/massage/upload/alyOssSignature' +
						`${object2Url(us)}`;
					const res = await this.$http.get({
						url: url,
						data: {
							us: us
						}
					});

					let markIndex = this.avatarObj.path.lastIndexOf('.');
					let suffix = this.avatarObj.path.substring(markIndex);
					let date = new Date().getTime();
					wx.uploadFile({
						url: res.data.host, //上传的路径
						filePath: this.avatarObj.path,
						name: 'file',
						formData: {
							name: this.avatarObj.path,
							key: date + suffix, //上传图片的名字和路径（默认路径根目录，自定义目录：xxx/xxx.png）
							policy: res.data.policy,
							OSSAccessKeyId: res.data.accessId,
							success_action_status: "200",
							signature: res.data.signature,
						},
						header: {
							'Access-Control-Allow-Origin': '*',
							'x-oss-callback-Authorization': 'access_token'
						},
						success: (result) => {
							console.log('chooseImage success, temp path is: ', res)

							let url = res.data.host + '/' + date + suffix;
							console.log(url)

							this.form.avatar = url
							if (url) {
								this.upLoadStatus = '上传成功'
							}
							wx.showToast({
								title: "上传成功",
								icon: 'success',
								duration: 1000
							})

						},
						fail: function({
							errMsg
						}) {
							console.log('upladImage fail, errMsg is: ', errMsg)
							wx.showToast({
								title: "上传失败",
								duration: 1000
							})
						},
					})

				}
			},
			getAddressResultObj(e) {
				//获取详细的 省/市/区 对象信息,更新时被动调,主动调用请自行设置ref值,方法一致
				console.log(e)
				this.form.city_name = e.city.name
				this.form.city_id = e.city.code
				this.form.area_name = e.county.name
				this.form.area_id = e.county.code
				this.form.province_name = e.province.name
				this.form.province_id = e.province.code

			},
			getCity(e) {
				console.log('获取城市的文字===', e);
				this.form.city = e;
			},

			// 选择日期
			bindDateChange: function(e) {
				this.date = e.target.value;
				console.log(e.target.value)
				this.form.birthday = e.target.value
				/* 在上面声明变量，split是用来截取字段*/
				this.yeartime = this.date.split('-')[0];
				this.monthtime = this.date.split('-')[1];
				this.daytime = this.date.split('-')[2];
			},
			// 日期获取
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}

				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},

		}
	};
</script>

<style lang="less">
	page {
		background-color: #ffffff;
	}

	.basicInformation {
		padding: 0 34rpx 50rpx;

		.myNmae {
			padding: 34rpx 0;
			background-color: #fff;
			min-width: 50rpx;

			.avatar {
				width: 84rpx;
				height: 84rpx;
			}

			input {
				text-align: right;
			}
		}

		.personalLntroduction {
			.textarea-box {
				margin-top: 24rpx;
				border: 2rpx dashed #1e6ceb;
				background: #fff;
				padding: 30rpx;
				border-radius: 20rpx;

				.textarea-color {
					text-align: right;
					color: #999;
				}
			}

			.sunui-pop-main {
				z-index: 900;
			}

			.describe {
				margin-left: 10rpx;
				color: #666;
				font-size: 14px;
			}

			margin-top: 50rpx;
		}

		.personalTags {
			margin: 36rpx 0;

			.tag_container {
				margin-top: 36rpx;
				display: flex;
				align-items: center;
				flex-direction: row;
				// justify-content: space-between;
				flex-wrap: wrap;
			}

			.zy_tag_add {
				width: 150rpx;
				height: 50rpx; //medium：45rpx；small：40rpx；mini：35rpx
				padding: 5rpx 15rpx;
				margin: 5rpx 8rpx;
				// border: 1rpx solid $zy-classic-grey;
				border-radius: 10rpx;
				text-align: center;
			}
		}

		.sumbit-btn {
			width: 92%;
			height: 100rpx;
			color: #ffffff;
			border-radius: 20rpx;
			background: #1890ff;
			position: fixed;
			bottom: 50rpx;
		}

		// “添加标签” 弹出框
		.v-pop-cls {
			padding: 46rpx 0 88rpx;
			width: 710rpx;
			height: auto;
			position: relative;
			border-radius: 20rpx;
			background: #fff;

			image {
				width: 52rpx;
				height: 52rpx;
				position: absolute;
				padding: 30rpx;
				top: 0rpx;
				right: 0rpx;
			}

			text {
				color: #000;
				font-size: 36rpx;
			}

			input {
				// text-align: center;
				padding: 0 20rpx;
				width: 608rpx;
				height: 94rpx;
				margin: 62rpx 0;
				border-radius: 20rpx;
				background: #f5f5f5;
			}

			view {
				width: 638rpx;
				height: 88rpx;
				background: #1890ff;
				color: #fff;
				font-size: 32rpx;
			}
		}
	}
</style>